<!--  -->
<template>
  <div id="app">
    <div
      class="item"
      v-for="item in list"
      :key="item.objectId"
      @click="link(item.link)"
    >
      <span>{{ item.publDate.substring(0, 16) }}</span>
      <span>{{ item.title }}</span>
    </div>
    <div class="item tc" v-show="status">暂无更多数据</div>
  </div>
</template>

<script>
import { CLIENT } from "@/common/const";
import http from "@/utils/http";

import localforage from "localforage";
const db = localforage.createInstance({
  name: "yngp-1",
});

export default {
  data() {
    return {
      list: [],
      urlParams: this.getParamsFromURL(),
      typeList: [
        "queryNewsList", //新闻
        "queryNoticeList", //公告
        "queryReportList", //研报
        "queryIndexNewsList", //查询新闻列表
      ],
      typeDetail: ["newsId", "noticeId", "reportId", "newsId"],
      detailIds: ["sourceId", "objectId", "objectId", "sourceId"],
      status: false,
    };
  },

  components: {},

  computed: {},

  mounted() {
    const set = new Set([
      "1-000001",
      "1-000905",
      "0-399001",
      "0-399006",
      "1-000300",
      "1-000016",
    ]);
    const { market, code } = this.urlParams;
    if (set.has(`${market}-${code}`)) {
      this.urlParams.type = 3;
    }
    this.getNewsList();
  },

  methods: {
    link(url) {
      window.prompt(
        "",
        `up://["open.zx-info-list",${Date.now()},{"url":"${url}","callbackId":${Date.now()}}]`
      );
    },
    getParamsFromURL(url = window.location.href) {
      if ("undefined" == url) {
        return {};
      }
      let paraString = url
        .substring(url.indexOf("?") + 1, url.length)
        .split("&");
      let paraObj = {};
      for (let i = 0; i < paraString.length; i++) {
        let j = paraString[i];
        paraObj[j.substring(0, j.indexOf("="))] = j.substring(
          j.indexOf("=") + 1,
          j.length
        );
      }
      return paraObj;
    },
    async getNewsList() {
      const self = this;
      const params = {
        stockCode: this.urlParams.code,
        limit: 20,
      };
      const type = this.typeList[this.urlParams.type];
      console.log(type, "type===");
      const requestTime = Date.now();
      const duration = 10 * 60 * 1000; //10分钟缓存时间
      const key = type + "-" + params.stockCode;
      const cache = await db.getItem(key);
      console.log(cache, "cache=====");
      if (cache && cache.saveTime + duration > requestTime) {
        this.list = cache.data;
        return;
      }
      // todo  start
      let url = `${CLIENT}/client/${type}.htm`;
      await http.get(url, params).then((back) => {
        console.log(back, "====back");

        if (back.status == 1) {
          if (back.data.length < 1) {
            self.status = true;
            return;
          }
          const data = back.data.map((item) => {
            item.link = `${CLIENT}/cilent/articleDetail.html?${
              self.typeDetail[self.urlParams.type]
            }=${item[self.detailIds[self.urlParams.type]]}`;
            return item;
          });
          console.log(data, "data===");
          db.setItem(key, { saveTime: Date.now(), data });
          self.list = data;
        }
      });
      // todo end
    },
  },
};
</script>
<style lang="scss" scoped></style>
